local CooldownWatchUnitTestsFrame = CreateFrame('Frame')
CooldownWatchUnitTestsVar = {}

local function nilled(s,...)
	s = not s and 'nil' or tostring(s)
	if select('#',...)>0 then
		return s, nilled(...)
	end
	return s
end
local function print(...)
	ChatFrame1:AddMessage('|cff33ff99CWUnitTests|r:'.. string.join(', ',nilled(...)))
end

function isNewToon()
	assert(CooldownWatch)
	assert(CooldownWatchVar, 'no database CooldownWatchVar')
	if not rawget(CooldownWatch, "GetConfigMode") then
		return false
	end
	local c = assert(CooldownWatch:GetConfigMode(), 'not in config mode')
	return c
end

function newToon()
	print('Testing on new toon.')
	assert(rawget(CooldownWatch, "AddWatch"), 'module InitConfig probably not loaded')

	assert(CooldownWatchAnchor)
	assert(CooldownWatchAnchor:IsShown())
	assert(CooldownWatchAnchor:IsVisible())

	print('Adding a spell by drag and drop.')
	assert(#CooldownWatch.frame==0)
	PickupSpell(1, 'spell')
	CooldownWatchAnchor:GetScript('OnMouseUp')(CooldownWatchAnchor, 'LeftButton')
	assert(#CooldownWatch.frame==1)
	local frame1 = CooldownWatch.frame[1]
	assert(frame1)

	print('Adding another spell by drag and drop.')
	assert(not CooldownWatch.frame[2])
	PickupSpell(2, 'spell')
	frame1:GetScript('OnMouseUp')(frame1, 'LeftButton')
	assert(#CooldownWatch.frame==2)
	-- the spell should be inserted at position 1
	local frame2 = CooldownWatch.frame[1]
	assert(frame2)
	assert(frame1==CooldownWatch.frame[2])

	print('Reordering spells by drag and drop.')
	frame2:GetScript('OnDragStart')(frame2)
	frame1:GetScript('OnEnter')(frame1)
	assert(frame1==CooldownWatch.frame[1])
	assert(frame2==CooldownWatch.frame[2])

	print('Error message expected for adding the same spell twice.')
	PickupSpell(2, 'spell')
	frame1:GetScript('OnMouseUp')(frame1, 'LeftButton')
	assert(#CooldownWatch.frame==2)

	print('Removing a spell.')
	frame1:GetScript('OnMouseUp')(frame1, 'MiddleButton')
	assert(#CooldownWatch.frame==1)

	print('Preparing to check the saved variable, reload the ui.')
	CooldownWatchUnitTestsVar.saved = { CooldownWatch.frame[1].watchname }
	CooldownWatchUnitTestsVar.checkSaved = true
end

function checkSaved()
	print('Checking the saved variable.')
	assert(CooldownWatchAnchor)
	assert(not CooldownWatchAnchor:IsShown())
	assert(not CooldownWatchAnchor:IsVisible())

	assert(CooldownWatch.frame[1].watchname == CooldownWatchUnitTestsVar.saved[1])

	CooldownWatchUnitTestsVar.saved = nil
	CooldownWatchUnitTestsVar.checkSaved = nil
end

local function OnEvent(frame, event, arg1, ...)
	if event == 'PLAYER_ENTERING_WORLD' then
		print('CooldownWatchUnitTests loaded, testing')
		local isNewToon = isNewToon()
		print('isNewToon', isNewToon)
		if isNewToon then
			newToon()
		elseif CooldownWatchUnitTestsVar.checkSaved then
			checkSaved()
			CooldownWatch:RemoveAll() CooldownWatch:Test()
		end
		print('All tests completed')
	end
end

function CooldownWatch:Test()
	self:ConfigMode(true)
	local _,race = UnitRace('player')
	if race=='Human' then
		CooldownWatch:AddSpellWatch("Jeder f\195\188r sich")
	end

	local _,class = UnitClass("player")
	if class == "WARLOCK" then
		--CooldownWatch:EnableDebug(3)
		w1 = CooldownWatch:AddSpellWatch("Verderbnis",{label = "20s"})
		w7 = CooldownWatch:AddWatch("debug", "Verderbnis",{label = "2h10s", duration=2*60*60+10})
		w3 = CooldownWatch:AddWatch("debug", "Verderbnis",{label = "2m10", duration=130})
		w4 = CooldownWatch:AddWatch("debug", "Verderbnis",{label = "15s", duration=15})
		w5 = CooldownWatch:AddWatch("debug", "Verderbnis",{label = "15s/90s", duration=90, remaining=15})
		w6 = CooldownWatch:AddWatch("debug", "Verderbnis",{label = "45s/90s", duration=90, remaining=45})
		w8 = CooldownWatch:AddWatch("debug", "Verderbnis",{label = "15s", duration=15, autohide = true})
		w9 = CooldownWatch:AddWatch("debug", "Verderbnis",{label = "15s", duration=15, autohidebar = true})
		w6.CheckCooldown()
	elseif class == "HUNTER" then
		w2 = CooldownWatch:AddSpellWatch("Arkaner Schuss")
		if UnitRace("player") == "Draenei" then
			w3 = CooldownWatch:AddSpellWatch("Gabe der Naaru")
		end
	elseif class == "WARRIOR" then
		local autohide = {autohide = true}
		w0 = CooldownWatch:AddSpellWatch("Spott")
		w = CooldownWatch:AddSpellWatch("Ersch\195\188tternder Schlag")
		w3 = CooldownWatch:AddSpellWatch("Einschreiten")
		w4 = CooldownWatch:AddSpellWatch("Abfangen")
		w7 = CooldownWatch:AddEquipWatch("trinket0", {label = "tk1"})
		w8 = CooldownWatch:AddEquipWatch("trinket1", {label = "tk2"})
		w2 = CooldownWatch:AddSpellWatch("Schildhieb", autohide)
		w5 = CooldownWatch:AddSpellWatch("Zauberreflexion", autohide)
		w9 = CooldownWatch:AddSpellWatch("Blutrausch", autohide)
		w6 = CooldownWatch:AddSpellWatch("Berserkerwut", autohide)
		w10 = CooldownWatch:AddSpellWatch("Herausforderungsruf", autohide)
		w10 = CooldownWatch:AddSpellWatch("Drohruf", autohide)
		w12 = CooldownWatch:AddSpellWatch("Letztes Gefecht", autohide)
		w11 = CooldownWatch:AddSpellWatch("Schildwall", autohide)
	elseif class == "PRIEST" then
		local autohide = {autohide = true}
		CooldownWatch:AddSpellWatch("Schattenwort: Tod")
		CooldownWatch:AddSpellWatch("Psychischer Schrei")
		CooldownWatch:AddEquipWatch("trinket0", {label = "tk1"})
		CooldownWatch:AddSpellWatch("Machtwort: Schild", autohide)
		CooldownWatch:AddSpellWatch("Verblassen", autohide)
		CooldownWatch:AddSpellWatch("Symbol der Hoffnung", autohide)
		--CooldownWatch:AddSpellWatch("Schattengeist", autohide)
	end
	self:ConfigMode(false)
end

function CooldownWatch:RemoveAll()
	while CooldownWatch.frame[1] do
		CooldownWatch:RemoveWatch(1)
	end
end

CooldownWatchUnitTestsFrame:SetScript('OnEvent', OnEvent)
CooldownWatchUnitTestsFrame:RegisterEvent('PLAYER_ENTERING_WORLD')
